﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using LeakView.common;

namespace LeakView.view
{
    public partial class ValueChart : UserControl
    {

        /// <summary>
        /// 正常背景色
        /// </summary>
        private static Color NormalColor = Color.LightCyan;
        /// <summary>
        /// 告警背景色
        /// </summary>
        private static Color WarningColor = Color.LightCoral;

        private TimeSpan maxTimeSpan = TimeSpan.FromMinutes(30);

        private ChartDataSource chartds = new ChartDataSource();

        private double thresholdHigh = double.NaN;
        [DescriptionAttribute("高报警阈值")]
        public double ThresholdHigh
        {
            get => thresholdHigh;
            set
            {
                this.thresholdHigh = value;
                updateThreshold();
            }
        }

        private double thresholdLow = double.NaN;
        [DescriptionAttribute("低报警阈值")]
        public double ThresholdLow
        {
            get => thresholdLow;
            set
            {
                this.thresholdLow = value;
                updateThreshold();
            }
        }

        public ValueChart()
        {
            InitializeComponent();
            this.chart1.Parent = this;


            if (!CSharpUtilities.IsDesignMode())
            {
                var chartArea = this.chart1.ChartAreas.First();
                chartArea.AxisX.Interval = 5D;
                chartArea.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
                chartArea.AxisX.LabelStyle.Format = "HH:mm";

                var time = DateTime.Now.AddHours(-1);
                chartds.Add(time, 70D);
                for (int i = 0; i < 100; i++)
                {
                    time = time + TimeSpan.FromSeconds(10);
                    chartds.Add(time, double.NaN).filterSpan(maxTimeSpan);
                }
                chartds.Bind(chart1);
            }

        }

        [Browsable(true)]
        [DescriptionAttribute("设置或获取图标标题")]
        public string LabelTitle
        {
            get => this.label1.Text;
            set => this.label1.Text = value;
        }

        public void appendValue(DateTime time, double value)
        {

            // 检查数据是否越界
            this.chart1.BackColor = checkValue(value) ? NormalColor : WarningColor;

            chartds
                .Add(time, value)
                .filterSpan(maxTimeSpan);

        }

        private void updateThreshold()
        {
            bool bhigh = double.IsNaN(this.ThresholdHigh);
            bool blow = double.IsNaN(this.ThresholdLow);
            if(bhigh && blow)
            {
                this.label3.Visible = false;
            }
            else
            {
                string thigh = bhigh ? "N" : ThresholdHigh.ToString("F3");
                string tlow = blow ? "N" : ThresholdLow.ToString("F3");
                this.label3.Text = tlow + "/" + thigh;
                this.label3.Visible = true;
            }
        }

        private bool checkValue(double value)
        {

            if (this.ThresholdHigh != double.NaN &&
                value != double.NaN &&
                value > this.ThresholdHigh)
                return false;

            if (this.ThresholdLow != double.NaN &&
                value != double.NaN &&
                value < this.ThresholdLow)
                return false;

            return true;

        }

        public void UpdateChart()
        {
            if (label2.InvokeRequired)
            {
                label2.Invoke(new Action(() => {
                    this.label2.Text = /*double.IsNaN(chartds.LastY) ? "----" : */chartds.LastY.ToString("F3");
                }));
            }
            else
            {
                this.label2.Text = /*double.IsNaN(chartds.LastY) ? "----" : */chartds.LastY.ToString("F3");
            }

            chartds.Bind(chart1);
        }

    }
}
